package com.nf.health.app.intervention_details.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.nf.health.app.R;
import com.nf.health.app.fragment.IBaseFragment;
import com.nf.health.app.intervention_details.adapter.CatalogueDetailsAdapter;
import com.nf.health.app.utils.DensityUtil;
import com.nf.health.app.utils.LogUtil;
import com.nf.health.app.utils.PreferenceHelper;
import com.nf.health.app.utils.ViewUtil;

/**
 * 方案目录
 *
 * @author changpeng
 *         16/11/10
 */
public class CatalogueDetailsFragment extends IBaseFragment {
    private RecyclerView mRecyclerView;
    private CatalogueDetailsAdapter mCatalogueDetailsAdapter;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        addViewFillInRoot(R.layout.common_layout_recycler);


        mCatalogueDetailsAdapter = new CatalogueDetailsAdapter(getActivity());
        mRecyclerView = ViewUtil.getView(rootView, R.id.recycler_view_common);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setAdapter(mCatalogueDetailsAdapter);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    public void onResume() {
        super.onResume();
        LogUtil.e("hepan" + "目录onResume >>hint" + getUserVisibleHint());
    }

//    @Override
//    public void setUserVisibleHint(boolean isVisibleToUser) {
//        super.setUserVisibleHint(isVisibleToUser);
//        if (isVisibleToUser) {
//            LogUtil.e("hepan", "显示目录" + Constants.CHANGE_ABLE.INTERVENTION_ID);
//        }
//    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    private void initDialog(){
        String guideFlag = PreferenceHelper.getString(PreferenceHelper.CATALOGUEYINDAO, "");
        if(TextUtils.isEmpty(guideFlag)){
            final Dialog dialog = new Dialog(getActivity(), R.style.Dialog_Fullscreen);
            LinearLayout ll = new LinearLayout(getContext());
            ll.setGravity(Gravity.CENTER);
            ImageView guideImage = new ImageView(getContext());
            guideImage.setImageResource(R.drawable.bg_catalogue_guide);
            guideImage.setLayoutParams(new LinearLayout.LayoutParams(DensityUtil.dip2px(getContext(),196),
                    DensityUtil.dip2px(getContext(),93),Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL));

            guideImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    PreferenceHelper.putString(PreferenceHelper.CATALOGUEYINDAO, "1");
                }
            });
            ll.addView(guideImage);
            dialog.setContentView(ll);
            dialog.show();
        }
    }

    /**
     * 对用户可见时执行 ，执行在onAttach之前
     * @param isVisibleToUser
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(isVisibleToUser){
            resume();
        }
    }

    private void resume(){
        //判断是否add 避免getActivity()出现空指针
        if(this.isAdded()){
            initDialog();
        }
    }
}
